本文档主要简述Rmarkdown的基本用法。
采用的是Markdown语法,需要快速浏览,可以看
插入链接 R Project
R Logo:
无序列表
有序列表
输出html,docx,pdf 需要编辑编辑YAML表头
分别是html、pdf、docx、beamer幻灯片(pdf)、ioslides幻灯片(html):
output: html_document html
output: pdf_document
output: word_document
output: beamer_presentation
output: ioslides_presentation
把r换成python,就可以嵌入python代码
1:20## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
具体的选项:
eval TRUE 是否估计代码的值及显示其结果
echo TRUE 显示结果时,是否也一同显示代码
warning TRUE 是否显示警告
error FALSE 是否显示错误
message TRUE 是否显示消息
tidy FALSE 是否调整代码的显示格式,使其更整洁
results “markup” “markup”(标记显示) , “asis”(文本显示) ,“hold”(末尾显示) 或 “hide”(隐藏)
cache FALSE 为了⽅便以后的提交,是否缓存结果
comment “##” 注释符号,用于结果之前
fig.width 7 由代码块⽣成的图表的宽度,英寸
fig.height 7 由代码块⽣成的图表的高度,英寸
点击Knit按钮
生成交互式报告,需要是html格式才可以;在yaml表头插入runtime: shiny;然后点击Run Document。 其实不用shiny,单纯用html+交互式图表也可以实现网页部分交互,如Plotly图库。
具体如本文件的yaml表头所示。 toc: yes表示输出目录,number_sections: yes表示目录有序号。
theme: readable主题。主题有“default” “cerulean” “journal” “flatly” “readable” “spacelab” “united” “cosmo”。
图形大小控制。在html_document下标注:fig_width: 7;fig_height: 6。
如果是自己加入CSS:
html_document:
css: my_own.css
计算
library(DT)
datatable(iris)summary(iris)## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
散点图
library(plotly)## Loading required package: ggplot2
## Warning in as.POSIXlt.POSIXct(Sys.time()): unknown timezone 'zone/tz/2021a.1.0/
## zoneinfo/Asia/Shanghai'
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
plot_ly(data = iris, x = "Sepal.Length", y = "Petal.Length",
color = "Species")## No trace type specified:
## Based on info supplied, a 'histogram2d' trace seems appropriate.
## Read more about this trace type -> https://plotly.com/r/reference/#histogram2d
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
时间序列图 #{r} #library(plotly) #p <- plot_ly(economics, x = "date", y = "uempmed", name = "unemployment") #p %>% add_trace(y = fitted(loess("uempmed" ~ as.numeric(date))), x = date) #
散点图
for (i in 1:10){
x=c((i+1):(i+10))
y=c((i+2):(i+11))
plot(x~y)
}